package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.system.domain.Ques_Collection;
import com.ruoyi.system.domain.vo.Results;
import com.ruoyi.system.service.Ques_CollectionService;
import com.ruoyi.web.controller.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class Ques_CollectionCollection {

    @Autowired
    private Ques_CollectionService ques_collectionService;

    //添加收藏
    @GetMapping("/saveQueCollection/{userid}/{quesid}")
    public R<String> saveQuesCollection(@PathVariable("userid") Integer userId,@PathVariable("quesid") Integer quesId){
        Ques_Collection ques_collection = new Ques_Collection();
        ques_collection.setUserId(userId);
        ques_collection.setQuesId(quesId);
        ques_collectionService.save(ques_collection);
        return R.success("添加成功");
    }
    @GetMapping("/SelectQueCollection/{userid}/{quesid}")
    public Results SelectQuesCollection(@PathVariable("userid") Integer userId, @PathVariable("quesid") Integer quesId){
        Ques_Collection one = null;
        LambdaQueryWrapper<Ques_Collection> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Ques_Collection::getQuesId,quesId).eq(Ques_Collection::getUserId,userId);
        one = ques_collectionService.getOne(lambdaQueryWrapper);
        if(one==null){
            return new Results(404,"查询失败",one);
        }
        return new Results(400,"查询成功",one);
    }
    @GetMapping("/SelectMyQueCollection/{userid}")
    public Results SelectMyQuesCollection(@PathVariable("userid") Integer userId){
        List<Ques_Collection> list=null;
        LambdaQueryWrapper<Ques_Collection> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Ques_Collection::getUserId,userId);
        list = ques_collectionService.list(lambdaQueryWrapper);
        if(list==null){
            return new Results(404,"查询失败",list);
        }
        return new Results(400,"查询成功",list);
    }
    //删除收藏
    @GetMapping("/deleteId/{id}")
    public R<String> deleteQueCollection(@PathVariable("id") int id){
        ques_collectionService.removeById(id);
        return R.success("删除成功");
    }
    //分页查询
    @PostMapping("/pageQueCollection")
    public R<Page>pageQueCollection(int page,int pageSize){

        Page<Ques_Collection> pageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Ques_Collection> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.orderByAsc(Ques_Collection::getId);

        ques_collectionService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }
    //


}
